// 对话框管理
import { ref } from "vue";

/**
 * 对话框管理对象
 */
export const dialogManager = ref({
  visible: false, //对话框是否可见
  topic: "", //对话框标题
  confirmButtonText: "确认", //确认按钮文字
  cancelButtonText: "取消", //取消按钮文字
  confirmCallback: null, //确认按钮回调函数
  cancelCallback: null, //取消按钮回调函数
});

/**
 * 重置对话框
 * 将dialogManager恢复到初始状态
 */
export const resetDialog = () => {
  dialogManager.value = {
    visible: false,
    topic: "",
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    confirmCallback: null,
    cancelCallback: null,
  };
};

/**
 * 显示对话框的工具方法
 * @param {string} topic - 对话框标题
 * @param {string} confirmButtonText - 确认按钮文字
 * @param {string} cancelButtonText - 取消按钮文字
 * @param {Function} confirmCallback - 确认按钮回调函数
 * @param {Function} cancelCallback - 取消按钮回调函数
 */
export const openDialog = (topic, confirmButtonText, cancelButtonText, confirmCallback, cancelCallback) => {
  dialogManager.value.visible = true;
  dialogManager.value.topic = topic;
  dialogManager.value.confirmButtonText = confirmButtonText;
  dialogManager.value.cancelButtonText = cancelButtonText;
  dialogManager.value.confirmCallback = () => {
    confirmCallback();
    resetDialog();
  };
  dialogManager.value.cancelCallback = () => {
    cancelCallback();
    resetDialog();
  };
};
